﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Bll;
using XZL.GM.Dal;



public partial class Admin_Course_CourseAdd : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            txtStartTime.Text = DateTime.Now.AddDays(+1).ToString("yyyy-MM-dd 21:00:00" )  ;
            BindDDL();
            BindTeamList();
        }

    }

    /// <summary>
    /// 绑定下拉列表
    /// </summary>
    private void BindDDL()
    { 
       ddlCourseType.DataSource=CourseInfoBiz.GetCourseTypeList();
       ddlCourseType.DataTextField = "CourseTypeName";
       ddlCourseType.DataValueField = "CourseTypeID";
       ddlCourseType.DataBind();

       ddlTeacherID.DataSource = CourseInfoBiz.GetCourseTeacherList();
       ddlTeacherID.DataTextField = "TeacherName";
        ddlTeacherID.DataValueField = "TeacherID";
        ddlTeacherID.DataBind();



    }

    private void BindTeamList()
    {
        int ? count=0;
        int i =Convert.ToInt32( ddlCourseType.SelectedValue);
        ddlTeam.DataSource = CourseInfoBiz.GetCourseTeamList(i, 1, 1000, ref count);
        ddlTeam.DataTextField = "TeamName";
        ddlTeam.DataValueField = "TeamID";
        ddlTeam.DataBind();
    }


    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnOK_Click(object sender,EventArgs e)
    {

        string price = txtPrice.Text.Trim();

        if (txtCourseName.Text.Trim() == "")
        {
            lblWarn.Text = "请输入课程名称！";
            txtCourseName.Focus();
            return;
        }

        //判断是否为时间格式
        string sTime = txtStartTime.Text.Trim();
        DateTime dtDate;
        if (DateTime.TryParse(sTime, out dtDate))
        {
            txtStartTime.Focus();
            
        }
        else
        {
            lblWarn.Text = "开始时间格式不对哦！";
            return;
        }

       

        double result;
        bool b = double.TryParse(price, out result);
        if (b)
        {
            lblWarn.Text = "";
        }
        else
        {
            lblWarn.Text = "请输入正确的课程价格！";
            txtPrice.Focus();
            return;
        }

        string sSort = txtSort.Text.Trim();
        int resulter;
        bool f = Int32.TryParse(sSort, out resulter);
        if (f)
        {
            lblWarn.Text = "";
        }
        else
        {
            lblWarn.Text = "请输入正确排序数字！";
            txtSort.Text = "0";
            txtSort.Focus();
            return;
        }

        string id = Request.QueryString["CourseID"];
        int courseID = Convert.ToInt32(id);
        string courseName = txtCourseName.Text.Trim();
        int courseType = Convert.ToInt32(ddlCourseType.Text.Trim());
        int courseTeamID = Convert.ToInt32(ddlTeam.Text.Trim());
        DateTime startTime = Convert.ToDateTime(txtStartTime.Text.Trim());


        string courseImg = lblFileNameImg.Text;
        string courseInfoImg = lblFileNameInfoImg.Text;
      
        int isCharge = Convert.ToInt32(ddlIsCharge.Text.Trim());
        int isLive = Convert.ToInt32(ddlIsLive.Text.Trim());
        int sort = Convert.ToInt32(txtSort.Text.Trim());
        int courseTeacher = Convert.ToInt32(ddlTeacherID.Text.Trim());
        string courseExplain = txtCourseExplain.Text.Trim();
        string courseIntroduce = txtCourseIntroduce.Text.Trim();
        string videoLink =txtVideoLink.Text.Trim();

        CourseInfo courseInfo = new CourseInfo()
        {
            CourseID= courseID, 
            CourseName = courseName,
            CourseType=courseType,
            CourseTeamID = courseTeamID,
            Price = Convert.ToDouble(price),
            StartTime = startTime,
           
            CourseImg = courseImg,
            CourseInfoImg=courseInfoImg,

            IsCharge =isCharge,
            IsLive=isLive,
            TeacherID = courseTeacher,
            Sort=sort,
            CourseExplain=courseExplain,
            CourseIntroduce = courseIntroduce ,
            VideoLink = videoLink
        };

     
        bool res = CourseInfoBiz.CourseAddition(courseInfo);
        if (res == true)
        {
            lblWarn.Text = "课程添加成功！";
            lblWarn.ForeColor = System.Drawing.Color.Green;
            btnOK.Enabled = false;
        }
        else
        {
            lblWarn.Text = "课程添加失败！";
            lblWarn.ForeColor = System.Drawing.Color.Red;
        }

        Response.Redirect("CourseList.aspx");
    }


    /// <summary>
    /// 上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        if (txtSelectFileOne.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFileOne.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;

                lblFileNameImg.Text = newName + fileExtension;         //列表图片
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfoone.Text = "上传列表图片成功";
                imgSignatureone.ImageUrl = imgPath + newName + fileExtension;    //列表图片

            }
        }
    }


    /// <summary>
    /// 上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoadone_Click(object sender, EventArgs e)
    {
        if (txtSelectFileTwo.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFileTwo.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;

 
                lblFileNameInfoImg.Text = newName + fileExtension;     //详情图片
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfotwo.Text = "上传详情图片成功";
                imgSignaturetwo.ImageUrl = imgPath + newName + fileExtension;    //详情图片
            }
        }
    }

    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("CourseList.aspx");
    }

    /// <summary>
    /// 绑定下拉事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlCourseType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindTeamList();
    }


    
}